package com.xxxcompany.wordbook.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.beanutils.BeanUtils;

import com.xxxcompany.wordbook.entity.User;
import com.xxxcompany.wordbook.entity.Word;
import com.xxxcompany.wordbook.service.IWordService;
import com.xxxcompany.wordbook.service.WordServiceImpl;

@WebServlet("/word_add")
public class WordAddServlet extends HttpServlet {
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setContentType("text/html;charset=utf-8");
		PrintWriter pw = resp.getWriter();
		
		
		HttpSession session = req.getSession();
		User u = (User)session.getAttribute("user");
		
		// 1. 
		Word w = new Word();
		try {
			BeanUtils.copyProperties(w, req.getParameterMap());
		} catch (IllegalAccessException | InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    w.setUserId(u.getId());
		// 2
		IWordService wordService = new WordServiceImpl();
		wordService.add(w);
		
		
		// 3.
		resp.sendRedirect("user_index");
		
		
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doGet(req, resp);
	}
}